<?php
namespace api\controllers;

use common\models\Active;
use common\models\ActiveAttr;
use common\models\GoodsAttr;
use common\models\SysHotWord;
use common\models\UserHotWord;
use common\service\ConstantService;
use common\service\GlobalUrlService;

class SearchController extends BaseController
{
    /**
     * 搜索结果页
     */
    public function actionGetLists(){
        if($this->isGet()){
            $page = intval($this->get('page'));
            $page = ( $page > 0 )? $page : 1;

            $search_word = $this->get('search_word');

            //排序默认1 2  销量 3 价格高到低  4 价格低到高
            $type = $this->get('type');
            switch ($type){
                case 1:
                    $sort = [
                        'set_top' => SORT_DESC ,
                        'sales_volume' => SORT_DESC,
                        'update_time' => SORT_DESC
                    ];
                    break;
                case 2:
                    $sort = [
                        'sales_volume' => SORT_DESC,
                    ];
                    break;
                case 3://价格从高到低
                    $sort = [
                        'max_price' => SORT_DESC,
                    ];
                    break;
                case 4://价格从低到高
                    $sort = [
                        'max_price' => SORT_ASC,
                    ];
                    break;
                default:
                    $sort = [
                        'set_top' => SORT_DESC ,
                        'sales_volume' => SORT_DESC,
                        'update_time' => SORT_DESC
                    ];
                    break;
            }
            $offset = ($page - 1) * $this->page_size;
            $active_query = Active::find();
            $active_query->andWhere(['status' => 1]);
            $active_query->andWhere(['is_del' => 0]);
            $active_query->andWhere(['like' ,'active_name',$search_word]);
            $lists = $active_query
                ->orderBy($sort)
                ->offset( $offset )
                ->limit( $this->page_size )
                ->asArray()
                ->all();
            $new_goods_attrs = [];
            if(!empty($lists)){
                $active_id = array_column($lists,'id');
                $active_attrs = ActiveAttr::find()
                    ->select(['active_id','original_price'])
                    ->where(['active_id'=>$active_id])
                    ->asArray()
                    ->all();
                $arr = [];
                foreach ($active_attrs as $attr){
                    if(in_array($attr['active_id'],$arr)){
                        array_push($arr,$attr['active_id']);
                    }
                    $new_goods_attrs[$attr['active_id']][] = $attr['original_price'];
                }
                unset($attr);
                foreach ($new_goods_attrs as $key=>$attr){
                    arsort($attr);
                    $new_goods_attrs[$key] = max($attr);
                }
                unset($attr);
            }
            foreach ($lists as &$list){
                $list['goods_img'] = GlobalUrlService::buildCdnUrl($list['goods_img']);
                if(isset($new_goods_attrs[$list['id']]) && !empty($new_goods_attrs[$list['id']])){
                    $list['max_price'] = $new_goods_attrs[$list['id']];
                }
            }

            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    /**
     * 添加热搜词
     */
    public function actionAddHotWord(){
        if($this->isGet()){
            //搜索关键词
            $search_type = $this->get('search_type'); // 1 用户 2 系统
            $search_word = $this->get('search_word');
            if(empty($search_type) || empty($search_word)){
                return $this->renderJson([],'参数错误',ConstantService::$response_fail);
            }
            if($search_type == 1){ //用户热词
                $user_id = $this->user['id'];
                $word = UserHotWord::findOne(['word'=>$search_word,'is_del' => 0,'user_id' => $user_id]);
                if(!empty($word)){
                    $word->search_times = $word['search_times'] + 1;
                    $word->update_time = time();
                    $re = $word->save();
                }else{
                    $data['word'] = $search_word;
                    $data['search_times'] = 1;
                    $data['user_id'] = $user_id;
                    $data['is_del'] = 0;
                    $data['create_time'] = time();
                    $data['update_time'] = time();
                    $word_query = new UserHotWord();
                    $word_query->setAttributes($data);
                    $re = $word_query->save();
                }
            }else{
                //系统热词
                $word = SysHotWord::findOne(['word'=>$search_word,'is_del' => 0]);
                $word->search_times = $word['search_times'] + 1;
                $word->update_time = time();
                $re = $word->save();
            }
            if($re){
                return $this->renderJson([],'保存成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'操作失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }

    /**
     * 获取系统热词
     */
    public function actionGetHotWords(){
        if($this->isGet()){
            $lists = SysHotWord::find()
                ->where(['is_del' => 0])
                ->orderBy(['search_times' => SORT_DESC])
                ->asArray()
                ->all();
            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 获取用户热词
     */
    public function actionGetUserWords(){
        if($this->isGet()){
            $conditions['is_del'] = 0;
            $conditions['user_id'] = $this->user['id'];
            $lists = UserHotWord::find()
                ->where($conditions)
                ->orderBy(['update_time' => SORT_DESC])
                ->offset(0)
                ->limit(12)
                ->asArray()
                ->all();
            return $this->renderJson($lists,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 删除用户热词
     */
    public function actionDelUserWords(){
        if($this->isGet()){
            $user_id = $this->user['id'];
            $re = UserHotWord::updateAll(['is_del'=>1],['user_id'=>$user_id]);
            if($re){
                return $this->renderJson([],'删除成功!',ConstantService::$response_success);
            }
            return $this->renderJson([],'删除失败',ConstantService::$response_fail);


        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);

    }



}